//
//  LRXFixPayPasswordView.m
//  路人侠
//
//  Created by zhangxulong on 5/27/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXFixAounctPaswordView.h"
#import "LRXPocketLoadData.h"
@interface LRXFixAounctPaswordView()<UITextFieldDelegate>
@property (nonatomic,weak) UITextField * verifyAuthText;
@property (nonatomic,weak) UIButton * getVerifyBtn;
@property (nonatomic,weak) CADisplayLink * link;
@property (nonatomic,assign) NSInteger  timer;
@property (nonatomic,weak) UITextField * pocketPassedField;
@property (nonatomic,weak) UITextField * lastPassedField;
@property (nonatomic,weak) UITextField * reNewPasswdField;
@property (nonatomic,strong) NSMutableArray * textFields;

@end

@implementation LRXFixAounctPaswordView

- (instancetype)init{
    self = [super init];
    self.backgroundColor = [UIColor whiteColor];
    CGFloat viewX = 35 * wScale;
    CGFloat viewW = MainScreenW - 2 * viewX;
    CGFloat viewY = 288 * hScale;
    CGFloat viewH = 720 * hScale;
    self.frame = CGRectMake(viewX, viewY, viewW, viewH);
    self.timer = 60;
    
    self.textFields = [NSMutableArray array];
    
    UILabel * headlabel = [[UILabel alloc]init];
    headlabel.text = @"修改账户密码";
    headlabel.font = [UIFont systemFontOfSize:28 * hScale];
    headlabel.frame = CGRectMake(36 * wScale, 30 * hScale, 0, 0);
    
    [headlabel sizeToFit];
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(headlabel.frame) + headlabel.frame.origin.y, self.frame.size.width, 2 * hScale)];
    lineView.backgroundColor = mainColor;
    
    
    UITextField * passwdText = [[UITextField alloc]initWithFrame:CGRectMake(65 * wScale, CGRectGetMaxY(lineView.frame) + 75 * hScale, self.frame.size.width - 65 * 2 * wScale, 30 * hScale)];
    passwdText.placeholder = @"请输入密码";
    passwdText.font = headlabel.font;
    passwdText.tag = 0;
    passwdText.returnKeyType = UIReturnKeyNext;
    passwdText.secureTextEntry = YES;
    passwdText.delegate = self;
    self.pocketPassedField = passwdText;
    UIView * lineView2 = [[UIView alloc]initWithFrame:CGRectMake(60 * wScale, CGRectGetMaxY(passwdText.frame) + 10 * hScale,self.frame.size.width - 120 * wScale, 2 * hScale)];
    lineView2.backgroundColor = GraylineColor;
    
    UITextField * newPasswdText = [[UITextField alloc]initWithFrame:CGRectMake(65 * wScale, CGRectGetMaxY(lineView2.frame) + 84 * hScale, self.frame.size.width - 65 * 2 * wScale, 30 * hScale)];
    newPasswdText.placeholder = @"请输入新的六位数密码";
    newPasswdText.font = headlabel.font;
    newPasswdText.tag = 1;
    newPasswdText.secureTextEntry = YES;
    newPasswdText.delegate = self;
    newPasswdText.returnKeyType = UIReturnKeyNext;
    self.lastPassedField = newPasswdText;
    UIView * lineView4 = [[UIView alloc]initWithFrame:CGRectMake(60 * wScale, CGRectGetMaxY(newPasswdText.frame) + 10 * hScale,self.frame.size.width - 120 * wScale, 2 * hScale)];
    lineView4.backgroundColor = GraylineColor;
    
    
    UITextField * reNewPasswdText = [[UITextField alloc]initWithFrame:CGRectMake(65 * wScale, CGRectGetMaxY(lineView4.frame) + 84 * hScale, self.frame.size.width - 65 * 2 * wScale, 30 * hScale)];
    reNewPasswdText.placeholder = @"确认新的六位数密码";
    reNewPasswdText.font = newPasswdText.font;
    reNewPasswdText.tag = 2;
    reNewPasswdText.secureTextEntry = YES;
    reNewPasswdText.delegate = self;
    self.reNewPasswdField = reNewPasswdText;
    UIView * lineView5 = [[UIView alloc]initWithFrame:CGRectMake(60 * wScale, CGRectGetMaxY(reNewPasswdText.frame) + 10 * hScale,self.frame.size.width - 120 * wScale, 2 * hScale)];
    lineView5.backgroundColor = GraylineColor;
    
    UIButton * accpetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [accpetBtn setTitle:@"确认" forState:UIControlStateNormal];
    accpetBtn.titleLabel.font = [UIFont systemFontOfSize:32 * hScale];
    [accpetBtn setTitleColor:mainColor forState:UIControlStateNormal];
    accpetBtn.frame = CGRectMake(CGRectGetMaxX(lineView4.frame) - 73 * wScale, CGRectGetMaxY(lineView5.frame) + 76 * hScale, 73 * wScale, 32 * hScale);
    [accpetBtn addTarget:self action:@selector(accpetClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:accpetBtn];
    [self addSubview:headlabel];
    [self addSubview:lineView];

    [self addSubview:passwdText];
    [self addSubview:reNewPasswdText];
    [self addSubview:lineView2];
    [self addSubview:lineView4];
    [self addSubview:newPasswdText];
    [self addSubview:lineView5];
    

    [self.textFields addObject:passwdText];
    [self.textFields addObject:newPasswdText];
    [self.textFields addObject:reNewPasswdText];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHide) name:UIKeyboardWillHideNotification object:nil];
    
    return self;
}
#pragma mark - 数据请求
- (void)accpetClick:(UIButton *)btn
{
    
    pocketLoadDataBlock loadDataBlock = ^(NSDictionary * parmDic){
        NSLog(@"%@",parmDic);
        NSInteger statusCode = [parmDic[@"data"][@"status_code"] integerValue];
        if (statusCode == 1) {
            
        }
        else if(statusCode == 2){
            [MBProgressHUD showError:@"请重新登录"];
        }
        else if(statusCode == 3){
            [MBProgressHUD showError:@"新密码和旧密码不能一致"];
        }
    
    };
    
    
    LRXPocketLoadData * loadData = [[LRXPocketLoadData alloc]initWithLoadDataBlock:loadDataBlock];
    NSString * newPassword = self.lastPassedField.text;
    NSString * reNewPassword = self.reNewPasswdField.text;
    NSDictionary * paramDic = @{@"password":newPassword,@"password_confirmation":reNewPassword};
    [loadData sessionDataTask:paramDic withUrl:resetPassUrl withMethod:@"POST"];
    
}
#pragma mark - 键盘配合textField的view
-(void)willShow:(NSNotification *)notifi{
    
    
    UITextField *currentTf = self.textFields[[self indexOfFirstResponder]];
    CGFloat maxY = CGRectGetMaxY(currentTf.frame) + self.frame.origin.y;
    
    
    //2.获取键盘的y值
    CGRect kbEndFrm = [notifi.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat kbY = kbEndFrm.origin.y;
    
    //3.进行比较
    //计算距离
    CGFloat delta = kbY - maxY - 70 * hScale;
    //需要往上移
    if(delta < 0){
        //添加个动画
        [UIView animateWithDuration:0.25 animations:^{
            self.transform = CGAffineTransformMakeTranslation(0, delta);
        }];
        
    }
}

/**
 *  隐藏键盘
 */
-(void)willHide{
    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformIdentity;
    }];
}

/**
 *  获取当前的响应者的索引
 */
-(NSInteger)indexOfFirstResponder{
    
    //isFirstResponder 判断对象是否时第一响应者
    
    for (UITextField * tf in self.textFields) {
        if (tf.isFirstResponder) {
            return tf.tag;
        }
    }
    //返回一1没有当前响应者
    return -1;
}
#pragma mark - UITextField 的代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if (textField.tag == 0) {
        UITextField * pp  = [self viewWithTag:1];
        [pp becomeFirstResponder];
    }
    else if(textField.tag == 1){
        UITextField * pp = [self viewWithTag:2];
        [pp becomeFirstResponder];
    }
    else if(textField.tag == 2)
    {
        [self endEditing:YES];
    }
    return YES;
}


@end
